package differences;

import javax.swing.JFileChooser;
import javax.swing.JFrame;

public class MainFrame extends JFrame {
	private static final long serialVersionUID = 1L;
	
	Main main;

	private javax.swing.JFileChooser chooser;
    private javax.swing.JButton close;
    private javax.swing.JButton compare;
    private javax.swing.JButton customize;
    private javax.swing.JButton openOldest;
    private javax.swing.JButton openYoungest;
	
    public MainFrame() {
        initComponents();
        setVisible(true);
    }


    private void initComponents() {

        chooser = new javax.swing.JFileChooser();
        customize = new javax.swing.JButton();
        compare = new javax.swing.JButton();
        close = new javax.swing.JButton();
        openOldest = new javax.swing.JButton();
        openYoungest = new javax.swing.JButton();

        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                closing(evt);
            }
        });

        customize.setFont(new java.awt.Font("Calibri", 0, 18));
        customize.setText("custumize parameters");
        customize.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                customizeActionPerformed(evt);
            }
        });

        compare.setFont(new java.awt.Font("Calibri", 0, 18));
        compare.setText("compare");
        compare.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                compareActionPerformed(evt);
            }
        });

        close.setFont(new java.awt.Font("Calibri", 0, 18)); // NOI18N
        close.setText("close");
        close.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                closeActionPerformed(evt);
            }
        });

        openOldest.setFont(new java.awt.Font("Calibri", 0, 18)); // NOI18N
        openOldest.setText("open oldest image");
        openOldest.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                openOldestActionPerformed(evt);
            }
        });

        openYoungest.setFont(new java.awt.Font("Calibri", 0, 18)); // NOI18N
        openYoungest.setText("open youngest image");
        openYoungest.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                openYoungestActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(compare)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 148, Short.MAX_VALUE)
                        .addComponent(close))
                    .addComponent(customize)
                    .addComponent(openYoungest)
                    .addComponent(openOldest))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(customize)
                .addGap(18, 18, 18)
                .addComponent(openYoungest)
                .addGap(18, 18, 18)
                .addComponent(openOldest)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 80, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(compare)
                    .addComponent(close))
                .addContainerGap())
        );

        pack();
    }

    private void customizeActionPerformed(java.awt.event.ActionEvent evt) {
        main.customizeParameters();
    }

    private void openYoungestActionPerformed(java.awt.event.ActionEvent evt) {
        if (chooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION){
            main.getYoungestImage(chooser.getSelectedFile());
        }
    }

    private void openOldestActionPerformed(java.awt.event.ActionEvent evt) {
        if (chooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION){
            main.getOldestImage(chooser.getSelectedFile());
        }
    }

    private void compareActionPerformed(java.awt.event.ActionEvent evt) {
        main.getDifferences();
    }

    private void closeActionPerformed(java.awt.event.ActionEvent evt) {
        dispose();
    }

    private void closing(java.awt.event.WindowEvent evt) {
        closeActionPerformed(null);
    }

    public void setCore(Main core) {
        this.main = core;
    }

    public void allowCustomizeParameters() {
        customize.setEnabled(true);
    }

    public void dontAllowCustomizeParameters() {
        customize.setEnabled(false);
    }

    public void allowGetYoungestImage() {
        openYoungest.setEnabled(true);
    }

    public void dontAllowGetYoungestImage() {
        openYoungest.setEnabled(false);
    }

    public void allowGetOldestImage() {
        openOldest.setEnabled(true);
    }

    public void dontAllowGetOldestImage() {
        openOldest.setEnabled(false);
    }

    public void allowGetDifferences() {
        compare.setEnabled(true);
    }

    public void dontAllowGetDifferences() {
        compare.setEnabled(false);
    }

}
